﻿package com.sense.canvas { 
	
	import flash.events.*;
	
	import org.wiiflash.Wiimote;
	import org.wiiflash.events.ButtonEvent;
	import org.wiiflash.events.WiimoteEvent;
	
	public class WiiCanvas extends Canvas { 
		
		public var myWiimote:Wiimote
		public var mySynchronize:Synchronize
		
		public function WiiCanvas() {
			trace("WiiCanvas Constructed")
			init()
		}
		
		public function init() {
			// create a new Wiimote
			myWiimote = new Wiimote();
			addEventListeners()
			
			trace("connecting to wiimote")
			// connect wiimote to WiiFlash Server
			myWiimote.connect ();
		}
		
		public function addEventListeners() {
			// register Wiimote button events
			
			myWiimote.addEventListener( Event.CONNECT, rumble );
			myWiimote.addEventListener( IOErrorEvent.IO_ERROR, handler );
			myWiimote.addEventListener( Event.CLOSE, handler );

			// myWiimote.addEventListener( ButtonEvent.A_PRESS, onAPressed );
			// myWiimote.addEventListener( ButtonEvent.A_RELEASE, onAReleased);
			//myWiimote.addEventListener( ButtonEvent.LEFT_PRESS, onLeftPressed );
			myWiimote.addEventListener( ButtonEvent.LEFT_RELEASE, onLeftReleased);
			//myWiimote.addEventListener( ButtonEvent.RIGHT_PRESS, onRightPressed );
			myWiimote.addEventListener( ButtonEvent.RIGHT_RELEASE, onRightReleased);
			//myWiimote.addEventListener( ButtonEvent.UP_PRESS, onUpPressed );
			myWiimote.addEventListener( ButtonEvent.UP_RELEASE, onUpReleased);
			//myWiimote.addEventListener( ButtonEvent.DOWN_PRESS, onDownPressed );
			myWiimote.addEventListener( ButtonEvent.DOWN_RELEASE, onDownReleased);
			
			// myWiimote.addEventListener( ButtonEvent.B_PRESS, onBPressed );
			// myWiimote.addEventListener( ButtonEvent.B_RELEASE, onBReleased);
			// myWiimote.addEventListener( ButtonEvent.MINUS_PRESS, onMinusPressed);
			// myWiimote.addEventListener( ButtonEvent.MINUS_RELEASE, onMinusReleased);
			// myWiimote.addEventListener( ButtonEvent.PLUS_PRESS, onPlusPressed);
			// myWiimote.addEventListener( ButtonEvent.PLUS_RELEASE, onPlusReleased);
			// myWiimote.addEventListener( ButtonEvent.HOME_PRESS, onHomePressed);
			// myWiimote.addEventListener( ButtonEvent.HOME_RELEASE, onHomeReleased);
			// myWiimote.addEventListener( ButtonEvent.ONE_PRESS, onOnePressed);
			// myWiimote.addEventListener( ButtonEvent.ONE_RELEASE, onOneReleased);
			// myWiimote.addEventListener( ButtonEvent.TWO_PRESS, onTwoPressed);
			// myWiimote.addEventListener( ButtonEvent.TWO_RELEASE, onTwoReleased);
			// myWiimote.addEventListener( WiimoteEvent.UPDATE, onUpdated );
			
			myWiimote.addEventListener( WiimoteEvent.UPDATE, onUpdated );
		}
		
		private function onRightReleased(e:Event) {
			super.goTo(super.setX-super.puls, super.setY)
		}
		
		private function onLeftReleased(e:Event) {
			super.goTo(super.setX+super.puls, super.setY)
		}
		
		private function onUpReleased(e:Event) {
			super.goTo(super.setX, super.setY+super.puls)	
		}
		
		private function onDownReleased(e:Event) {
			super.goTo(super.setX, super.setY-super.puls)	
		}

		/*
		private function onPlusPressed(e:Event) {
			scaleAt(1.06, 1000, 1000)
		}
		
		private function onMinusPressed(e:Event) {
			scaleAt(0.94, mouseX, mouseY)
		}
		*/
		
		public function rumble(e:Event) {
			myWiimote.rumbleTimeout = 1000;
		}
		
		public function handler(e:Event) { 
			trace(e)
		}
		
		public function onAPressed(e:Event) {
			trace("a was pressed")
		}
		
		function onUpdated ( pEvt:WiimoteEvent ):void {
			trace(myWiimote.sensorX)
			
			var mp = 60
			goTo(x + myWiimote.sensorX * mp , y + myWiimote.sensorY * mp  )

			
			//	sensorZ
			if (myWiimote.sensorX > 0) {
				//goTo(x + myWiimote.sensorX * mp , y )
			}
			
			if(myWiimote.sensorX < 0) {
				//goTo(x + myWiimote.sensorX * mp, y )
			}
			
			/*
			if (myWiimote.sensorY > 0) {
				goTo(x, y - myWiimote.sensorY * mp)
			}
			
			if(myWiimote.sensorY < -0) {
				goTo(x, y + myWiimote.sensorY * mp)
			}
			*/
		}
	}
}